package com.hf.lookatme.customview;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hf.lookatme.R;

/**
 * 描述：自定义的顶栏
 * 创建人：maizi
 * 创建日期：2015/6/30
 * 修改人：JoeShiguang
 * 修改日期：2015/7/1
 * 备注：默认返回键和操作按钮都是gone，当设置listener时可见，或调用setXXXVisibility时可见
 * @version 2.0
 */
public class HeaderView extends RelativeLayout {

  private TextView textView_title;

  private ImageView imageView_back;

  private TextView textView_operation;

  private ImageView imageView_operation;

  public HeaderView(Context context) {
    super(context);
  }

  public HeaderView(Context context, AttributeSet attrs) {
    super(context, attrs);
    View view = View.inflate(context, R.layout.layout_actionbar, null);
    addView(view, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    initView();
  }

  private void initView() {

    textView_title = (TextView) findViewById(R.id.textView_actionBar_title);
    imageView_back = (ImageView) findViewById(R.id.imageView_actionBar_back);
    textView_operation = (TextView) findViewById(R.id.textView_actionBar_operation);
    imageView_operation = (ImageView) findViewById(R.id.imageView_actionBar_operation_useIcon);
  }

  /**
   * 设置标题
   *
   * @param title
   */
  public void setTitle(String title) {

    this.textView_title.setText(title);
  }

  public void setTitle(int titleId) {

    String title = getResources().getString(titleId);
    this.textView_title.setText(title);
  }

  /**
   * 设置返回按钮可见性
   *
   * @param visibility
   */
  public void setBackVisibility(int visibility) {

    this.imageView_back.setVisibility(visibility);
  }

  /**
   * 设置操作Icon可见性
   *
   * @param visibility
   */
  public void setOperationIconVisibility(int visibility) {

    this.imageView_operation.setVisibility(visibility);
  }

  /**
   * 设置操作Text可见性
   *
   * @param visibility
   */
  public void setOperationTextVisibility(int visibility) {

    this.textView_operation.setVisibility(visibility);
  }

  /**
   * 设置ActionBar返回点击Listener
   *
   * @param listener
   */
  public void setOnBackClickListener(final onClickListener listener) {

    this.imageView_back.setVisibility(View.VISIBLE);
    this.imageView_back.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        listener.onClick();
      }
    });
  }

  /**
   * 设置操作Icon点击Listener
   *
   * @param listener
   */
  public void setOnOperationIconClickListener(final onClickListener listener) {

    this.imageView_operation.setVisibility(View.VISIBLE);
    this.imageView_operation.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        listener.onClick();
      }
    });
  }

  /**
   * 设置操作Text点击Listener
   *
   * @param listener
   */
  public void setOnOperationTextClickListener(String text, final onClickListener listener) {

    this.textView_operation.setText(text);
    this.textView_operation.setVisibility(View.VISIBLE);
    this.textView_operation.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        listener.onClick();
      }
    });
  }

  /**
   * 设置操作Text点击Listener
   *
   * @param listener
   */
  public void setOnOperationTextClickListener(int textId, final onClickListener listener) {

    String text = getResources().getString(textId);
    this.textView_operation.setText(text);
    this.textView_operation.setVisibility(View.VISIBLE);
    this.textView_operation.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        listener.onClick();
      }
    });
  }

  /**
   * ActionBar按钮点击事件
   */
  public interface onClickListener {
    void onClick();
  }
}